package cn.remex.core.quartz;

import cn.remex.RemexConstants;
import cn.remex.core.exception.NestedException;
import cn.remex.core.exception.ServiceCode;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;

import java.util.Map;


/** 
 * @author liuhengyang 
 * date 2014-10-14 上午11:58:38
 * @version 版本号码
 */
public abstract class Job implements org.quartz.Job {
	protected Map<?, ?> mJobParams = null;
	
	public void execute(JobExecutionContext ctx) throws JobExecutionException {
		mJobParams = getJobParams(ctx);
		long start = System.currentTimeMillis(),end=0;
		String schedulerName;
		try {
			jobExecute(ctx);
			end = System.currentTimeMillis();
			schedulerName = ctx.getScheduler().getSchedulerName();
		} catch (NestedException e) {
			RemexConstants.logger.error("捕获到业务异常:{}", e);
			throw e;
		} catch (Exception e) {
			RemexConstants.logger.error("Remex框架中没有处理的异常:{}", e);
			throw new QuartzException(ServiceCode.FAIL, "Remex框架中没有处理的异常", e);
		}
		RemexConstants.logger.info("quartz日程[{}]执行任务完成[{}],耗时[{}]ms", schedulerName,ctx.getJobDetail().getKey(),end-start);
	}
	
	public Map<?, ?> getJobParams(JobExecutionContext cxt) {
		return cxt.getJobDetail().getJobDataMap();
	}
	
	public abstract void jobExecute(JobExecutionContext cxt) throws JobExecutionException;

}
